/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.beltrafficguider.manager;

import br.com.beltrafficguider.model.Bus;
import com.colorfulsoftware.atom.Entry;
import com.colorfulsoftware.atom.Feed;
import com.colorfulsoftware.atom.FeedDoc;
import java.net.URL;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Carlos Pantoja
 * @since 2011-09-09
 */
public class BlogReaderManager {

    private static final String FEED_URL = "http://rotadosonibus.blogspot.com/feeds/posts/default";
    private static final String TITLE_STR = "LINHA";

    public List<Bus> loadFeedsFromURL() {
        List<Bus> busList = null;
        try {
            // Receive all feeds from URL
            Feed myFeed = new FeedDoc().readFeedToBean(new URL(FEED_URL));

            // Initialize bus list
            busList = new ArrayList<Bus>();

            // Fetch all entries
            for (Entry entry : myFeed.getEntries()) {
                if (entry.getTitle().getText().contains(TITLE_STR)) {
                    String[] title = null;
                    // Break title by ':' and '-' to get the bus number and name
                    if(entry.getTitle().getText().contains("–")) {
                        title = entry.getTitle().getText().split(":")[1].split("–");
                    } else {
                        title = entry.getTitle().getText().split(":")[1].split("-");
                    }

                    // Initialize Bus object
                    Bus bus = new Bus();

                    // Set bus line id
                    bus.setBusLineId(title[0].trim());

                    // Check if the bus name has '-' and concat them if does
                    if (title.length == 2) {
                        bus.setBusLineName(title[1].trim());
                    } else {
                        StringBuilder stb = new StringBuilder("");
                        for (int i = 1; i < title.length; i++) {
                            if (i != title.length - 1) {
                                stb.append(title[i]).append("-");
                            } else {
                                stb.append(title[i]);
                            }
                        }
                        bus.setBusLineName(stb.toString().trim());
                    }

                    System.out.println("Bus (Line Id - Name):");
                    System.out.print(bus.getBusLineId() + " - ");
                    System.out.println(bus.getBusLineName());
                    System.out.println("");

                    // Get the route content and break it by ':'
                    String[] content = entry.getContent().getContent().split(":");
                    // Check if the line has two routes
                    if (content.length < 12) {
                        // Getting info of busses which has one route
                        bus.setFirstRouteA(content[1].split("&lt;")[0].trim());
                        bus.setSecondRouteA(content[3].split("&lt;")[0].trim());
                        bus.setEndOfLineA(content[6].split("&lt;")[0].trim());

                        System.out.println("Bus First Route A:");
                        System.out.println(bus.getFirstRouteA());
                        System.out.println("Bus Second Route A:");
                        System.out.println(bus.getSecondRouteA());
                        System.out.println("Bus End Of Line A:");
                        System.out.println(bus.getEndOfLineA());
                    } else {
                        // Getting info of busses which has two routes
                        bus.setFirstRouteA(content[2].split("&lt;")[0].trim());
                        bus.setSecondRouteA(content[4].split("&lt;")[0].trim());
                        bus.setEndOfLineA(content[5].split("&lt;")[0].trim());
                        bus.setFirstRouteB(content[7].split("&lt;")[0].trim());
                        bus.setSecondRouteB(content[9].split("&lt;")[0].trim());
                        bus.setEndOfLineB(content[12].split("&lt;")[0].trim());

                        System.out.println("Bus First Route A:");
                        System.out.println(bus.getFirstRouteA());
                        System.out.println("Bus Second Route A:");
                        System.out.println(bus.getSecondRouteA());
                        System.out.println("Bus End Of Line A:");
                        System.out.println(bus.getEndOfLineA());

                        System.out.println("Bus First Route B:");
                        System.out.println(bus.getFirstRouteB());
                        System.out.println("Bus Second Route B:");
                        System.out.println(bus.getSecondRouteB());
                        System.out.println("Bus End Of Line B:");
                        System.out.println(bus.getEndOfLineB());
                    }

                    // Get feed publish date
                    bus.setPublishDate(entry.getPublished().getDateTime());
                    // Get feed last update
                    bus.setUpdateDate(entry.getUpdated().getDateTime());


                    System.out.println("Publish Date:");
                    System.out.println(DateFormat.getDateInstance().format(bus.getPublishDate()));
                    System.out.println("Update Date:");
                    System.out.println(DateFormat.getDateInstance().format(bus.getUpdateDate()));

                    // Add bus to the bus list
                    busList.add(bus);
                }
            }
        } catch (Exception e) {
        }

        return busList;
    }
}